﻿using AuthCenter_HuangJingtao.Domain;
using AuthCenter_HuangJingtao.ErrorCode;
using AuthCenter_HuangJingtao.Infrastructure.Imterfaces;
using AuthCenter_HuangJingtao.Read.Api.Application.Command.MenuaLL;
using MediatR;

namespace AuthCenter_HuangJingtao.Read.Api.Application.CommandHandler.MenuaLL
{
    public class AddMenusCommandHandler : IRequestHandler<AddMenusCommand, ApiResult<int>>
    {
        IBaseReponsitory<Menus> Menus;

        public AddMenusCommandHandler(IBaseReponsitory<Menus> Menus)
        {
            this.Menus = Menus;
        }

        public async Task<ApiResult<int>> Handle(AddMenusCommand request, CancellationToken cancellationToken)
        {
            if (request.menus.Menu_Name == "")
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "菜单名称不为空",
                };
            }
            if (request.menus.Menu_Url == "")
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "跳转的URl地址不为空",
                };
            }
            var Tst = Menus.GetQuery().Where(x => x.Menu_Name == request.menus.Menu_Name && !x.Menu_IsDel).ToList();
            if (Tst.Count > 0)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "菜单名称已存在",
                    Data = 0
                };
            }
            else 
            {
           request.menus.Create_Date = DateTime.Now;
            await Menus.AddAsync(request.menus);
            return new ApiResult<int>
            {
                Code = ApiEnum.Success,
                Message = "添加菜单成功",
                Data = 1
            };
            }

     

        }
    }
}
